package com.kasax.skin;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;

import com.google.android.material.tabs.TabLayout;
import com.kasax.skin.adapter.MyFragmentPagerAdapte;
import com.kasax.skin.fragments.MusicFragment;
import com.kasax.skin.fragments.StationFrament;
import com.kasax.skin.fragments.VideoFragment;
import com.kasax.skin.support.utils.CommonUtils;
import com.kasax.skin.support.utils.FileUtils;
import com.kasax.skin.support.utils.LogUtils;
import com.kasax.skin.support.utils.permissions.PermissionConstants;
import com.kasax.skin.support.utils.permissions.PermissionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MainActivity extends AppCompatActivity {

    private List<Fragment> fragments = new ArrayList<>();
    private List<String> pageTitles = new ArrayList<>();

    private ViewPager viewPager;
    private TabLayout tabLayout;
    private boolean hasSdcardPermission = false;
    private PermissionUtils.HandleCallback handleCallback;

    private ExecutorService executorService = Executors.newCachedThreadPool();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initData();

        PermissionUtils.injectContext(this);
        handleCallback = () -> {
            hasSdcardPermission = true;
            LogUtils.i(getClass(), "handleCallback hasSdcardPermission: " + hasSdcardPermission);
            if (hasSdcardPermission) {
                executorService.execute(new Runnable() {
                    @Override
                    public void run() {
                        LogUtils.i(getClass(), "写入测试文件");
                        String safeSdcardPath = FileUtils.getSafeSdcardPath("/mnt/sdcard/test1.txt");
                        LogUtils.e("safeSdcardPath: " + safeSdcardPath);
                        FileUtils.write("hello 12414124", safeSdcardPath);

                        LogUtils.i(getClass(), "写入测试文件");
                        String content = FileUtils.readString(safeSdcardPath);
                        LogUtils.e("content: " + content);
                    }
                });
            }
        };
        PermissionUtils.requestSdcardPermission(handleCallback);
    }

    private void initData() {
        tabLayout.setupWithViewPager(viewPager);
        fragments.add(new VideoFragment());
        fragments.add(new MusicFragment());
        fragments.add(new StationFrament());
        pageTitles.addAll(Arrays.asList("视频", "音乐", "电台"));
        viewPager.setAdapter(new MyFragmentPagerAdapte(getSupportFragmentManager(), fragments, pageTitles));
    }

    private void initView() {
        viewPager = findViewById(R.id.viewpager);
        tabLayout = findViewById(R.id.tab_layout);
    }

    public void handleCustomSkin(View view) {
        startActivity(new Intent(this, CustomSkinActivity.class));
    }

    private List<Integer> permissionCaches = new ArrayList<>();

    @Override
    protected void onResume() {
        super.onResume();

        try {
            Activity currentShowActivity = CommonUtils.getCurrentShowActivity();
            String packageName = currentShowActivity.getPackageName();
            if (packageName.equals(CommonUtils.getCurrentApp().getPackageName())) {
                if (!permissionCaches.isEmpty()) {
                    PermissionUtils.handleIntentPermissionRequestResult(permissionCaches.get(0), permissionCaches.get(1), handleCallback);
                    permissionCaches.clear();
                }
            }
        } catch (Exception e) {
            LogUtils.e(getClass(), e);
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        permissionCaches.add(requestCode);
        permissionCaches.add(resultCode);

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionUtils.handleOnRequestPermissionsResult(requestCode, permissions, false, handleCallback);
    }
}